package com.zfjt.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ClientmessageBean;
import com.zfjt.entity.ReturnBean;
import com.zfjt.packnum.PackNum00011;
import com.zfjt.service.ClientmessageBeanService;
import com.zfjt.utils.WQUtil;

/**
 * 传车子照片
 * @author xk
 * @version 1.0
 * @date 2017年4月05日
 */
@Controller
public class AddImg03Controller {
	
	private ReturnBean mReturn;
	
	private WQUtil wqUtil;
	
	@Autowired
	private ClientmessageBeanService mClientMessage;
	
	private Map<String, String> fhdata; 
	
	@RequestMapping(value="00011")
	@ResponseBody
	public ReturnBean AddImg03(HttpSession hseesion,HttpServletRequest request){
		wqUtil = new WQUtil();
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00011 num00011 = JSON.parseObject(data,PackNum00011.class); 	
		ClientmessageBean mdata = mClientMessage.selectByPrimaryKey((short) Integer.parseInt(num00011.getId()));
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00011");
			fhdata.put("error", "TOKEN_ERROR");
			mReturn.setData(JSON.toJSONString(fhdata));
		}else{	
			//获取Tomcat下项目根目录
			String path = request.getSession().getServletContext().getRealPath("static\\images" + "/") +"\\" + mdata.getName();
			//根据用户名新建文件夹存相应照片
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			wqUtil.GenerateImage(num00011.getImg03_01(),path + "\\" + "Img03_01" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_02(),path + "\\" + "Img03_02" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_03(),path + "\\" + "Img03_03" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_04(),path + "\\" + "Img03_04" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_05(),path + "\\" + "Img03_05" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_06(),path + "\\" + "Img03_06" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_07(),path + "\\" + "Img03_07" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_08(),path + "\\" + "Img03_08" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_09(),path + "\\" + "Img03_09" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_10(),path + "\\" + "Img03_10" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_11(),path + "\\" + "Img03_11" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_12(),path + "\\" + "Img03_12" + ".jpg");
			wqUtil.GenerateImage(num00011.getImg03_13(),path + "\\" + "Img03_13" + ".jpg");
			//把图片路径存入
			mdata.setImg0301(path + "\\" + "Img03_01" + ".jpg");
			mdata.setImg0302(path + "\\" + "Img03_02" + ".jpg");
			mdata.setImg0303(path + "\\" + "Img03_03" + ".jpg");
			mdata.setImg0304(path + "\\" + "Img03_04" + ".jpg");
			mdata.setImg0305(path + "\\" + "Img03_05" + ".jpg");
			mdata.setImg0306(path + "\\" + "Img03_06" + ".jpg");
			mdata.setImg0307(path + "\\" + "Img03_07" + ".jpg");
			mdata.setImg0308(path + "\\" + "Img03_08" + ".jpg");
			mdata.setImg0309(path + "\\" + "Img03_09" + ".jpg");
			mdata.setImg0310(path + "\\" + "Img03_10" + ".jpg");
			mdata.setImg0311(path + "\\" + "Img03_11" + ".jpg");
			mdata.setImg0312(path + "\\" + "Img03_12" + ".jpg");
			mdata.setImg0313(path + "\\" + "Img03_13" + ".jpg");
			//把完成的接口步骤记录下来
			String str = mdata.getIsaccomplish();
			mdata.setIsaccomplish(str + ",img03");
			mClientMessage.updateByPrimaryKeySelective(mdata);
			mReturn.setCode("000");
			fhdata.put("succeed", "1");
			mReturn.setData(JSON.toJSONString(fhdata));
			mReturn.setPortnum("00011");
		}
		return mReturn;
	}
}
